﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;

namespace VersionAutoupgradeServices
{
    /// <summary>
    /// version_ajax_data 的摘要说明
    /// </summary>
    public class version_ajax_data : IHttpHandler
    {
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string str = context.Request.QueryString["arg"];
            DirectoryInfo di = new DirectoryInfo(context.Server.MapPath("~/Versions/" + str + "/Updates/"));
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(context.Server.MapPath("~/Versions/" + str + "/version.xml"));
            XmlNode xmlNode = xmlDoc.SelectSingleNode("//Items");
            xmlNode.RemoveAll();
            this.GetFileFromDirectory(di, (XmlElement)xmlNode, xmlDoc, string.Empty);
            context.Response.Write(xmlDoc.OuterXml);
        }

        public void GetFileFromDirectory(DirectoryInfo di, XmlElement element, XmlDocument xmlDoc, string prex)
        {
            foreach (DirectoryInfo di1 in di.GetDirectories())
            {
                string prex1 = di1.FullName.Substring(di.FullName.Length);
                this.GetFileFromDirectory(di1, element, xmlDoc, prex1);
            }
            foreach (FileInfo fileInfo in di.GetFiles())
            {
                XmlElement element1 = xmlDoc.CreateElement("Item");
                element1.SetAttribute("name", fileInfo.Name);
                element1.SetAttribute("file", string.IsNullOrEmpty(prex) ? fileInfo.Name : prex + "\\" + fileInfo.Name);
                element1.SetAttribute("version", fileInfo.LastWriteTime.ToString("yyyyMMddHHmmss"));
                element1.SetAttribute("size", fileInfo.Length.ToString());
                element1.SetAttribute("type", fileInfo.Extension);
                element.AppendChild((XmlNode)element1);
            }
        }


    }
}